﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using xraiding.Models;
using xraiding.ViewModels;

namespace xraiding.Controllers
{
    [Authorize]
    public class CharacterManagerController : Controller
    {
        //
        // GET: /CharacterManager/
        public ActionResult Index()
        {
            return View(new CharacterInfoViewModel());
        }

        public ActionResult GettingStarted()
        {
            return View(new CharacterEditViewModel());
        }

        [HttpPost]
        public ActionResult GettingStarted(CharacterEditViewModel character)
        {
            character.Save();
            return RedirectToAction("UserPreferences", "CharacterManager");
        }

        public ActionResult UserPreferences()
        {
            return View(new UserPreferencesViewModel());
        }

        [HttpPost]
        public ActionResult UserPreferences(UserPreferencesViewModel preferencesViewModel)
        {
            preferencesViewModel.Save();
            return RedirectToAction("Index", "XRaiding");
        }

        public JsonResult GetRoles(int classId)
        {
            return Json(XRaidingRepository.GetRoles(classId), JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetClasses()
        {
            return Json(XRaidingRepository.GetClasses(), JsonRequestBehavior.AllowGet);
        }
        
        public ActionResult GetNewTimeAvailability(int index)
        {
            var availableTimeViewModel = new AvailableTimeSlotViewModel(index);
            availableTimeViewModel.LoadDataFromDB();
            return PartialView("_NewTimeAvailabilityRecord", availableTimeViewModel);
        }
            
        public ActionResult NewCharacter()
        {
            return View("EditCharacter", new CharacterEditViewModel());
        }

        [HttpPost]
        public ActionResult NewCharacter(CharacterEditViewModel model)
        {
            model.Save();
            return RedirectToAction("Index");
        }

        public ActionResult EditCharacter(int id)
        {
            return View(new CharacterEditViewModel(id));
        }

        [HttpPost]
        public ActionResult EditCharacter(CharacterEditViewModel model)
        {
            model.Save();
            return RedirectToAction("Index");
        }

        public ActionResult DeleteCharacter(int id)
        {
            CharacterEditViewModel.DeleteCharacter(id);
            return RedirectToAction("Index");
        }
    }
}
